package com.zengge.zbc.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtils {

    private final static Logger logger = LoggerFactory.getLogger(JsonUtils.class);

    private static ObjectMapper objectMapper;
    static {
        objectMapper = new ObjectMapper();
    }

    public static String toJson(Object object){
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            logger.error(e.getMessage());
            return null;
        }
    }

    public static <T>T toObject(String json,Class<T> clazz) {
        try {
            return objectMapper.readValue(json,clazz);
        }catch (JsonProcessingException e){
            logger.error(e.getMessage());
            return null;
        }
    }
}
